local print = print
if not print then
	-- in wow
	print = function (s)
		ChatFrame1:AddMessage(s)
	end
else
	-- not in wow
	dofile("core.lua")
	dofile("utils.lua")
	dofile("functions.lua")
	dofile("classes.lua")
	dofile("vclasses.lua")
	
	if not CreateFrame then
		local frame_mt = {
			__index = function (self, index)
				if index == "CreateTexture" then
					return function (self, name, layer) return CreateFrame("Texture", name, layer) end
				elseif index == "CreateFontString" then
					return function (self, name, layer) return CreateFrame("FontString", name, layer) end
				end
				return function (self, ...) print("Frame:"..index.."()", ...) end
			end,
		}

		CreateFrame = function (class, name, template)
			local f = setmetatable({
				class = class,
				name = name,
				template = template,
			}, frame_mt)
			if name then
				_G[name] = f
			end
			return f
		end
		UnitHealthMax = function () return 100 end
		UnitManaMax = function () return 100 end
		UnitHealth = function () return 100 end
		UnitMana = function () return 100 end
		UnitPowerType = function () return 1 end
		SetPortraitTexture = function () end
		GetPlayerBuffTexture = function () end
	end
	dump = function (s) f = io.open("test_result.lua", "w") f:write(s) f:close() end
end

local duf = _G.DeclarativeUnitFrames

local v = duf:Make{
	class = "Button",
	parent = "UIParent",
	template = "SecureUnitButtonTemplate",
	attributes = {
		unit = "$arg1",
		type1 = "target",
	},
	width = 170,
	height = 50,
	toplevel = true,
	movable = true,
	clique = true,
	children = {
		healthbar = {
			class = "StatusBar",
			width = "$parent:GetWidth()",
			height = 18,
			max = "${UnitHealthMax}($arg1)",
			value = "${UnitHealth}($arg1)",
			color = "green",
			texture = "Interface\\TargetingFrame\\UI-StatusBar",
			anchors = {
				{ "TOPLEFT", "$parent", "TOPLEFT" }
			},
			children = {
				text = {
					class = "FontString",
					template = "GameFontNormal",
					text = "${UnitHealth}($arg1)..' / '..${UnitHealthMax}($arg1)",
					anchors = "all",
				},
			},
		},
		portrait = {
			class = "Texture",
			portrait_texture = "$arg1",
			width = 32,
			height = 32,
			anchors = {
				{ "TOPRIGHT", "$parent", "TOPLEFT" }
			},
		},
		manabar = {
			class = "StatusBar",
			width = "$parent:GetWidth()",
			height = 12,
			max = "${UnitManaMax}($arg1)",
			value = "${UnitMana}($arg1)",
			color = "${UnitPowerType}($arg1) == 0 and \"blue\" or \"red\"",
			texture = "Interface\\TargetingFrame\\UI-StatusBar",
			anchors = {
				{ "TOPLEFT", "$parent", "TOPLEFT", 0, -18 }
			},
			children = {
				text = {
					class = "FontString",
					template = "GameFontNormal",
					text = "${UnitMana}($arg1)..' / '..${UnitManaMax}($arg1)",
					anchors = "all",
				},
			},
		},
		buffs = {
			class = "PlayerAuraContainer",
			anchors = {
				{ "TOPLEFT", "$parent", "TOPRIGHT", 0, 0 }
			},
			width=16*40,
			height=16,
			child_template = {
				class = "Button",
				width = 16,
				height = 16,
				scripts = {
					OnEnter = "text_OnEnter",
					OnLeave = "text_OnLeave",
				},
				text_OnEnter = [[
function (self) 
	GameTooltip:ClearLines()
	GameTooltip:SetOwner(self)
	GameTooltip:SetPlayerBuff(self:GetID())
end
]],
				text_OnLeave = [[
function (self)
	if GameTooltip:IsOwned(self) then
		GameTooltip:Hide()
	end
end
]],
				children = {
					texture = {
						class = "Texture",
						texture = "${GetPlayerBuffTexture}(frame:GetParent():GetID())",
						anchors = "all",
					},
					text = {
						class = "FontString",
						template = "GameFontNormalSmall",
						texture = "${GetPlayerBuffApplications}(frame:GetParent():GetID())",
						anchors = "all",
					},
				},
			},
			max = "40",
			filter = "HELPFUL",
			layout = [[
function (parent, child, id)
	child:SetPoint("TOPLEFT", parent, (id - 1) * 16, 0)
end
]],
		}
	},
}

if dump then dump(v) end
v = assert(loadstring(v))()

local status, result = pcall (v, "player")
if not status then
	print(result)
else
	_G.DufSampleFrame = result
	result:SetPoint("BOTTOMLEFT", 500, 500)
end
